package com.main.hotel.service.impl;

import com.main.hotel.entity.Customer;
import com.main.hotel.repository.CustomerRepository;
import com.main.hotel.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class ManageServiceImpl implements ManageService {
    
    @Autowired
    private CustomerRepository customerRepository;
    
    @Override
    public int addCustomer(Customer customer) {
        customerRepository.save(customer);
        return 1;
    }

    @Override
    public int deleteCustomerByCustomer_id(Integer customer_id) {
        //customerRepository.deleteCustomerByCustomer_id(customer_id);
        customerRepository.deleteById(customer_id);
        return 1;
    }

    @Override
    public int updateCustomer(Customer customer) {
        Customer customer1=new Customer();
        customer1.setCustomer_id(customer.getCustomer_id());
        customer1.setUsername(customer.getUsername());
        customer1.setEmail(customer.getEmail());
        customer1.setCreate_date(customer.getCreate_date());
        customer1.setPhone(customer.getPhone());
        customer1.setSex(customer.getSex());
        customer1.setId_card(customer.getId_card());
        customer1.setPassword(customer.getPassword());
        customer1.setPet_name(customer.getPet_name());
        customerRepository.save(customer1);
        return 1;
    }

    @Override
    public Page<Customer> findAll(int page, int pageSize) {
        Pageable pageable= PageRequest.of(page,pageSize);
        return customerRepository.findAll(pageable);
    }

    @Override
    public Customer findByCustomerId(int customer_id) {
        Customer customer=customerRepository.getCustomerById(customer_id);
        return customer;
    }
}
